package com.lagou.controller;

import com.lagou.domain.*;
import com.lagou.service.MenuService;
import com.lagou.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;


    /*查询所有角色（条件）*/
    @RequestMapping("/findAllRole")
    public ResponseResult findAllRole(@RequestBody Role role) {

        List<Role> allRole = roleService.findAllRole(role);
        ResponseResult responseResult = new ResponseResult(true, 200, "查询所有成功", allRole);
        return responseResult;
    }

    /*菜单*/
    @Autowired
    private MenuService menuService;

    /*查询所有的父子菜单信息（分配菜单的第一个接口）*/
    @RequestMapping("/findAllMenu")
    public ResponseResult findSubMenuListByPid() {

        //-1表示查询所有的父级菜单，而后查询子级菜单
        List<Menu> menus = menuService.findSubMenuListByPid(-1);

        //响应数据格式对照
        Map<String, Object> map = new HashMap<>();
        map.put("parentMenuList", menus);

        ResponseResult responseResult = new ResponseResult(true, 200, "菜单数据查询成功", map);
        return responseResult;
    }

    /*根据角色ID查询关联的菜单信息ID*/
    @RequestMapping("/findMenuByRoleId")
    public ResponseResult findMenuByRoleId(Integer roleId) {

        List<Integer> menuByRoleId = roleService.findMenuByRoleId(roleId);
        ResponseResult responseResult = new ResponseResult(true, 200, "查询角色ID关联菜单信息ID成功", menuByRoleId);
        return responseResult;
    }

    /*为角色分配菜单*/
    @RequestMapping("/RoleContextMenu")
    public ResponseResult roleContextMenu(@RequestBody RoleMenuVo roleMenuVo) {
        System.out.println(roleMenuVo.getRoleId());
        System.out.println(roleMenuVo.getMenuIdList());
        roleService.roleContextMenu(roleMenuVo);
        ResponseResult responseResult = new ResponseResult(true, 200, "响应成功", null);
        return responseResult;
    }

    /*删除角色*/
    @RequestMapping("/deleteRole")
    public ResponseResult deleteRole(Integer id) {
        roleService.deleteRole(id);
        ResponseResult responseResult = new ResponseResult(true, 200, "删除角色成功", null);
        return responseResult;
    }

    /*获取当前角色拥有的资源信息和资源分页*/
    @RequestMapping("/findResourceListByRoleId")
    public ResponseResult findResourceListByRoleId(Integer roleId) {
        List<ResourceCategory> resourceListByRoleId = roleService.findResourceListByRoleId(roleId);
        return new ResponseResult(true, 200, "获取当前角色拥有的资源成功", resourceListByRoleId);
    }

    /*为角色分配资源*/
    @RequestMapping("/roleContextResource")
    public ResponseResult roleContextResource(@RequestBody RoleResourceVO roleResourceVO) {
        roleService.roleContextResource(roleResourceVO);
        ResponseResult responseResult = new ResponseResult(true, 200, "分配资源成功过", null);
        return responseResult;
    }

    @RequestMapping("/saveOrUpdateRole")
    public ResponseResult saveOrUpdateRole(@RequestBody Role role) {
        ResponseResult responseResult = null;
        if (role.getId() == null) {
            roleService.saveRoles(role);
            responseResult = new ResponseResult(true, 200, "新增成功", null);
        } else {
            roleService.updateRoles(role);
            responseResult = new ResponseResult(true, 200, "修改成功", null);
        }
        return responseResult;
    }
}
